﻿
Imports System.Net
Imports std
Imports webctrl

Public Module helper
    Public Function generateXhtmlPage(ByVal context As httpContext) As xhtmlpage
        assert(Not context Is Nothing, "context is nothing.")
        Dim rtn As xhtmlpage = Nothing
        rtn = context.createControl(New xhtmlpage())
        Dim lang1 As String = Nothing
        Dim lang2 As String = Nothing
        languageSeparate(context.language(), lang1, lang2)

        Dim c As String = Nothing
        If Not isemptystring(lang1) AndAlso Not isemptystring(lang2) Then
            c = lang1 + character.blank + lang2
        ElseIf Not isemptystring(lang1) Then
            c = lang1
        ElseIf Not isemptystring(lang2) Then
            c = lang2
        End If

        If Not isemptystring(c) Then
            If isemptystring(rtn.body().class) Then
                rtn.body().class += character.blank
            End If
            rtn.body().class += c
        End If

        Return rtn
    End Function

#If RETIRED Then
    Public Sub languageSeparate(ByVal context As httpContext, _
                                ByRef full As String, ByRef market As String, ByRef language As String)
        assert(Not context Is Nothing, "context is nothing.")
        copy(full, context.language())
        languageSeparate(full, market, language)
    End Sub
#End If

    Public Function parseQuery(ByVal context As HttpListenerContext,
                               ByRef results As vector(Of pair(Of String, String)),
                               Optional ByVal lowercase As Boolean = False,
                               Optional ByVal e As Text.Encoding = Nothing) As Boolean
        If context Is Nothing OrElse context.Request Is Nothing Then
            Return False
        End If

        On Error Resume Next
        If isemptystring(context.Request.Url.Query) Then
            Return True
        End If

        Return urlUtils.parseQueryString(context.Request.Url.Query, results, lowercase, e)
    End Function

    Public Function parseUrlEncodedInputStream(ByVal context As HttpListenerContext,
                                               ByRef results As vector(Of pair(Of String, String)),
                                               Optional ByVal lowercase As Boolean = False,
                                               Optional ByVal e As Text.Encoding = Nothing,
                                               Optional ByVal action As pointer(Of callbackAction) = Nothing,
                                               Optional ByVal timeoutticks As Int64 = npos) As Boolean
        If context Is Nothing OrElse context.Request Is Nothing OrElse
           context.Request.InputStream Is Nothing OrElse Not context.Request.InputStream.CanRead Then
            Return False
        End If

        If results Is Nothing Then
            results = New vector(Of pair(Of String, String))()
        Else
            results.clear()
        End If

        If Not context.Request.HasEntityBody() Then
            Return True
        End If

        If action Is Nothing Then
            Dim reader As IO.StreamReader = Nothing
            Try
                reader = New IO.StreamReader(context.Request.InputStream, _
                                             ifoper(e Is Nothing, context.Request.ContentEncoding, e))
            Catch ex As Exception
                raiseError("cannot create StreamReader for context.Request.InputStream, " + ex.Message _
                           , errorHandle.errorType.system)
                Return False
            End Try

            Dim str As String = Nothing
            Try
                str = reader.ReadToEnd()
            Catch ex As Exception
                raiseError("cannot read data from context.Request.InputStream. " + ex.Message,
                           errorHandle.errorType.system)
                Return False
            End Try

            Return urlUtils.parseQueryString(str, results, lowercase, e)
        Else
            Dim readAction As callbackAction = Nothing
            Dim result As pointer(Of String) = Nothing
            result = New pointer(Of String)()
            If readstreamstring3(context.Request.InputStream,
                                 action:=readAction,
                                 result:=result,
                                 holdtime:=ticksToMilliseconds(timeoutticks),
                                 e:=ifoper(e Is Nothing, context.Request.ContentEncoding, e)) Then
                Dim rst As vector(Of pair(Of String, String)) = Nothing
                rst = results
                action.set(New callbackAction(check:=callbackAction.action_check(
                                                        readAction,
                                                        Function() As Boolean
                                                            Return urlUtils.parseQueryString(+result, rst, lowercase, e)
                                                        End Function),
                                              timeoutticks:=timeoutticks))
                Return begin(action.get())
            Else
                Return False
            End If
        End If
    End Function
End Module
